package MyExcelTool;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class Parser {
	public static void parse(String template,File file){
		parse(template,file,"sheet0");
	}
	public static void parse(String template,File file,String sheetname){
		InputStream is = null;
		Workbook wb = null;
		Reader reader=null;
		
		try {
			is=new FileInputStream(file);
			wb=Workbook.getWorkbook(is);
			
			/*
			String[] names=wb.getSheetNames();
			for(int i=0;i<names.length;i++){
				System.out.println(names[i]);
			}
			*/
			
			Sheet sheet=wb.getSheet(sheetname);
			if(sheet==null) throw new Exception("\nWow!\nIt Seems Like That There is No Sheet Found By Given Sheet Name.\nWould You Check The Sheet That is Really Exist?");
			
			reader=(Reader)Class.forName(template).newInstance();
			reader.setSheet(sheet);
			reader.read();
			
			wb.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println(e.toString());
		}
	}
}
